import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    islogin: false, //保存是否已登录成功
    username: "", //用户名
    nickname: "", //昵称
    avatar: {}, //头像
    phone: "", //电话号码
    customerServiceList:[], //客服对话
    myCart:[], //购物车
    favorite:[],//收藏夹
    news: [], //新闻详情内容
    likenumInt : [],// 点赞数
    remainingSum: 100,// 钱包余额
    bill:[ 
      {
        state:1,
        price:100,
        describe:"赠送 - 新用户赠送100元",
        time:"2022年02月06日 16:58",
        details:{
          time:"2022-02-06 16:58:43",
          commodity:[
            { name:"赠送",num:1,price:"100.00" },
          ]
        }
      }
    ], //账单
  },
  mutations: {
    //this.$store.commit('loginOK','xx')自动执行
    // 登录后保存用户登录信息
    loninOK(state, newname) {
      state.islogin = newname[0] ? true : false;
      state.username = newname[0];
      state.nickname = newname[1];
      state.avatar = newname[2]? require(`../assets/avatar/${newname[2]}`): "";
      state.phone = newname[3];
    },
    // 客服对话
    CustomerService(state, newlist){
      state.customerServiceList = newlist;
    },
    // 修改客服对话状态
    upServiceState(state, num){
      state.customerServiceList[num].showState = false;
    },
    // 加入购物车
    addCart(state, newid){
      if (state.myCart.some(item=>item.id == newid)) {
        state.myCart.some(item=>{
          if (item.id == newid) {
            item.count += 1;
          }
        })
      }else{
        let originPrice = Math.round(Math.round(15 +  newid.substring(0,3) *0.01).toFixed(2)/0.9).toFixed(2);
        let price = (originPrice*0.9).toFixed(2);
        state.myCart.push({id:parseInt(newid),count: 1,price,originPrice});
      }
    },
    // 删除购物车
    decreaseCart(state, newid){
      state.myCart.some((item,index) =>{
        if (item.id == newid) {
          item.count -= 1;
          if (item.count < 1 ) {
            state.myCart.splice(index,1);
          }
        }
      })
    },
    // 购物车删除整个商品
    delCart(state, index){
      state.myCart.splice(index, 1);
    },
    // 购物车商品下单后删除
    buyCart(state, arr){
      arr.sort((a, b) => a - b);       
      // 数组删除多个元素 ,要从后往前逐个删除
      for (let index = arr.length - 1; index >= 0; index--) {
        state.myCart.splice(arr[index], 1);
      }
    },
    //加入收藏夹
    addFavorite(state, newval) {
      // 收藏夹没有的商品才能存
      if (state.favorite.indexOf(newval) === -1) {
        state.favorite.push(newval);
      }
    },
    //删除收藏夹 根据下标
    delFavorite(state, index) {
      // 根据下标删除对应商品
      state.favorite.splice(index, 1);
    },
    //删除收藏夹 根据商品id
    delFavoriteID(state, id) {
      if (state.favorite.indexOf(id) !== -1) {
        let index = state.favorite.indexOf(id);
        // 根据下标删除对应商品
        state.favorite.splice(index, 1);
      } 
    },
    //新闻详情内容
    new(state, newval) {
      state.news = newval
    },
    // 点赞
    addLike(state, newid){
      if (state.likenumInt.some(item=>item.id == newid)) {
        state.likenumInt.some(item=>{
          if (item.id == newid) {
            item.count += 1;
          }
        })
      }else{
        state.likenumInt.push({id:parseInt(newid),count: parseInt(newid)+1});
      }
    },
    // 钱包充值
    addMoney(state, money){
      state.remainingSum += money;
      state.remainingSum = state.remainingSum.toFixed(2)*1;
    },
    // 添加账单
    addBill(state, money){
      let obj = {
        state:money[0],
        price:money[1],
        describe:money[2],
        time:money[3],
        details:money[4]
      }
      state.bill.unshift(obj);
    }
  },
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
    }),
  ],
})